# listings/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser # 用于自定义用户模型
from django.conf import settings # 用于获取 AUTH_USER_MODEL

# 1. 自定义用户模型 (CustomUser)
class CustomUser(AbstractUser):
    # 用户类型选项
    USER_TYPE_CHOICES = [
        ('student', '学生'),
        ('teacher', '教师'),
        ('staff', '教职工'),
    ]
    # 继承自 AbstractUser，它已经包含了 username, email, password, first_name, last_name, is_staff, is_active, date_joined 等字段
    user_type = models.CharField(
        max_length=10,
        choices=USER_TYPE_CHOICES,
        default='student',
        verbose_name='用户类型'
    )
    student_id = models.CharField(max_length=20, unique=True, verbose_name='学号/工号')
    college = models.CharField(max_length=50, verbose_name='学院')
    major = models.CharField(max_length=50, verbose_name='专业/部门')
    bio = models.TextField(max_length=500, blank=True, verbose_name='个人简介')
    # avatar = models.ImageField(upload_to='avatars/', null=True, blank=True, verbose_name="头像") # 头像功能后续可以添加

    # 如果你的自定义字段需要在创建超级用户时被询问，需要添加到 REQUIRED_FIELDS
    # USERNAME_FIELD 默认是 'username'
    # REQUIRED_FIELDS = ['email', 'student_id'] # email 已经是 AbstractUser 的 REQUIRED_FIELDS 之一

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return self.username

# 2. 技能模型 (Skill)
class Skill(models.Model):
    REWARD_TYPE_CHOICES = [
        ('free', '免费互助'),
        ('exchange', '技能交换'),
        ('paid_virtual', '虚拟币/积分'), # 平台虚拟货币
        ('paid_cash', '少量现金(线下)'),
        ('negotiable', '面议'),
    ]
    CATEGORY_CHOICES = [ # 简单的分类示例，可以根据实际情况扩展
        ('tech', '技术（编程、软件等）'),
        ('design', '设计（PS、视频剪辑等）'),
        ('language', '语言（口语、翻译等）'),
        ('academic', '学术（论文润色、课程辅导等）'),
        ('life', '生活（乐器、维修、跑腿等）'),
        ('sports', '运动（陪练、教学等）'),
        ('other', '其他'),
    ]
    provider = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='provided_skills', verbose_name="提供者")
    title = models.CharField(max_length=200, verbose_name="技能名称/标题")
    description = models.TextField(verbose_name="技能描述")
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='other', verbose_name="技能分类")
    reward_type = models.CharField(max_length=20, choices=REWARD_TYPE_CHOICES, default='free', verbose_name="回报方式")
    reward_description = models.CharField(max_length=255, blank=True, verbose_name="回报具体说明 (如交换什么技能/金额)")
    tags = models.CharField(max_length=255, blank=True, verbose_name="标签 (逗号分隔)") # 简单标签系统
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_active = models.BooleanField(default=True, verbose_name="是否可见") # 管理员可以禁用

    class Meta:
        verbose_name = "技能"
        verbose_name_plural = "技能"
        ordering = ['-created_at'] # 默认按创建时间倒序

    def __str__(self):
        return f"{self.title} (由 {self.provider.username})"

# 3. 需求模型 (Need)
class Need(models.Model):
    NEED_CATEGORY_CHOICES = [
        ('study', '学习辅导'),
        ('life', '生活服务'),
        ('tech', '技术支持'),
        ('activity', '活动组织'),
        ('other', '其他'),
    ]
    
    REWARD_TYPE_CHOICES = [
        ('free', '免费'),
        ('money', '现金报酬'),
        ('exchange', '技能互换'),
        ('other', '其他'),
    ]

    title = models.CharField('标题', max_length=100)
    description = models.TextField('描述')
    category = models.CharField('分类', max_length=20, choices=NEED_CATEGORY_CHOICES)
    reward_type = models.CharField('回报方式', max_length=20, choices=REWARD_TYPE_CHOICES)
    reward_description = models.TextField('回报说明', blank=True)
    tags = models.CharField('标签', max_length=100, blank=True, help_text='用空格分隔多个标签')
    requester = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='needs')
    is_active = models.BooleanField('是否有效', default=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        verbose_name = '需求'
        verbose_name_plural = '需求'
        ordering = ['-created_at']

    def __str__(self):
        return self.title